//
//  XCObserver.m
//  StudyDemo01
//
//  Created by 心晨 on 2024/6/2.
//

#import "XCObserver.h"

@interface XCObserver ()

@property (nonatomic, copy) void (^observerChangeCallBack)(NSDictionary *change);

@property (nonatomic, copy) NSString *keyPath;

@end

@implementation XCObserver


- (instancetype)initWithObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context block:(void(^)(NSDictionary *change))block
{
    self = [super init];
    if (self) {
        _observerChangeCallBack = block;
        _keyPath = keyPath;
        [observer addObserver:self forKeyPath:keyPath options:options context:context];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (self.observerChangeCallBack) {
        self.observerChangeCallBack(change);
    }
}

@end
